#include "speechmanager.h"

SpeechManager::SpeechManager(QObject *parent)
    : QObject(parent),
      isListening(false),
      speechSession(nullptr)
{
}

SpeechManager::~SpeechManager()
{
    speech_recognizer_destroy_session(&speechSession);
}

bool SpeechManager::initialize()
{
    // 创建会话
    speechSession = speech_recognizer_create_session();
    if (!speechSession) {
        qWarning() << "创建语音失败";
        return false;
    }
    // 初始化会话
    if (speech_recognizer_init_session(speechSession) != 0) {
        qWarning() << "语音初始化失败";
        speech_recognizer_destroy_session(&speechSession);
        return false;
    }
    SpeechModelConfig *modelconfig = speech_model_config_create();
//    speech_model_config_set_name(modelconfig,"百度-语⾳⼤模型");
    if(!modelconfig){
        qWarning() << "获取模型失败";
        return false;
    }
    speech_model_config_set_name(modelconfig,"讯飞-语⾳⼤模型");
    speech_model_config_set_deploy_type(modelconfig,ModelDeployType::PublicCloud);
    speech_recognizer_set_model_config(speechSession, modelconfig);
    // 默认麦克风
    AudioConfig *a_config = audio_config_create_continuous_audio_input_from_default_microphone();
    if(!a_config){
        qWarning() << "获取麦克风失败";
        return false;
    }
    audio_config_set_input_audio_rate(a_config, 16000);
    speech_recognizer_set_audio_config(speechSession,a_config);

    // 设置结果回调
    speech_recognizer_result_set_callback(speechSession, speechCallback, this);

    return true;
}

void SpeechManager::startListening()
{

//    if (isListening) {
//        return;
//    }
    speech_recognizer_start_continuous_recognition_async(speechSession);
    isListening = true;
//    emit recognitionStatusChanged(true);
}

void SpeechManager::stopListening()
{
//    if (!isListening) {
//        return;
//    }
    speech_recognizer_stop_continuous_recognition_async(speechSession);
    isListening = false;
//    emit recognitionStatusChanged(false);
}


void SpeechManager::speechCallback(SpeechRecognitionResult *result, void *user_data)
{
    if (!result) {
        qDebug() <<"没有回调结果";
        return;
    }
    SpeechManager *manager = static_cast<SpeechManager*>(user_data);
    if (!manager){
        qDebug() <<"配置存在问题";
        return;
    }
    int resultType = speech_recognition_result_get_reason(result);
    const char *resultData = speech_recognition_result_get_text(result);
    int speakerId = speech_recognition_result_get_speaker_id(result);
    if(resultType == 1){
//        manager->recognitionData.text = "识别失败！";
        manager->recognitionData.error=true;
        emit manager->recognitionResult(manager->recognitionData.error);
    }
    else if (resultType == 2) {
        manager->recognitionData.text = "";
        manager->recognitionData.isFinal = false;
    }
    else if (resultType == 3) {
        manager->recognitionData.text.append(QString::fromUtf8(resultData));
        manager->recognitionData.isFinal = false;
    }
    else if (resultType == 4) {
        manager->recognitionData.text = QString::fromUtf8(resultData);
        manager->recognitionData.isFinal = true;
    }
    else if (resultType == 5) {
        manager->recognitionData.isFinal = true;
    }

    qDebug() << "resultType=" << resultType
             << ", speakerId=" << speakerId;

    //emit manager->recognitionResult(manager->recognitionData.text);
    emit manager->recognitionResult(manager->recognitionData.isFinal);

}

